package com.suep.game_chat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suep.game_chat.entity.BrowseHistory;
import com.suep.game_chat.entity.ContentFilter;
import com.suep.game_chat.entity.UserPreference;
import com.suep.game_chat.mapper.BrowseHistoryMapper;
import com.suep.game_chat.mapper.ContentFilterMapper;
import com.suep.game_chat.mapper.UserPreferenceMapper;
import com.suep.game_chat.service.BehaviorService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class BehaviorServiceImpl implements BehaviorService {

    private final BrowseHistoryMapper browseHistoryMapper;
    private final UserPreferenceMapper userPreferenceMapper;
    private final ContentFilterMapper contentFilterMapper;

    @Override
    @Transactional
    public void recordBrowseHistory(BrowseHistory history) {
        history.setBrowseTime(LocalDateTime.now());
        browseHistoryMapper.insert(history);
    }

    @Override
    public List<BrowseHistory> getBrowseHistory(String userId, int page, int size) {
        Page<BrowseHistory> pageParam = new Page<>(page, size);
        QueryWrapper<BrowseHistory> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId)
                .orderByDesc("browse_time");
        return browseHistoryMapper.selectPage(pageParam, wrapper).getRecords();
    }

    @Override
    @Transactional
    public void clearBrowseHistory(String userId) {
        QueryWrapper<BrowseHistory> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        browseHistoryMapper.delete(wrapper);
    }

    @Override
    @Transactional
    public void deleteBrowseHistory(Long historyId) {
        browseHistoryMapper.deleteById(historyId);
    }

    @Override
    @Transactional
    public void saveUserPreference(UserPreference preference) {
        preference.setCreateTime(LocalDateTime.now());
        preference.setUpdateTime(LocalDateTime.now());
        userPreferenceMapper.insert(preference);
    }

    @Override
    public List<UserPreference> getUserPreferences(String userId) {
        QueryWrapper<UserPreference> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId)
                .eq("is_enabled", true);
        return userPreferenceMapper.selectList(wrapper);
    }

    @Override
    @Transactional
    public void updateUserPreference(UserPreference preference) {
        preference.setUpdateTime(LocalDateTime.now());
        userPreferenceMapper.updateById(preference);
    }

    @Override
    @Transactional
    public void deleteUserPreference(Long preferenceId) {
        userPreferenceMapper.deleteById(preferenceId);
    }

    @Override
    @Transactional
    public void addContentFilter(ContentFilter filter) {
        filter.setCreateTime(LocalDateTime.now());
        filter.setUpdateTime(LocalDateTime.now());
        contentFilterMapper.insert(filter);
    }

    @Override
    public List<ContentFilter> getContentFilters(String userId) {
        QueryWrapper<ContentFilter> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId)
                .eq("is_enabled", true);
        return contentFilterMapper.selectList(wrapper);
    }

    @Override
    @Transactional
    public void updateContentFilter(ContentFilter filter) {
        filter.setUpdateTime(LocalDateTime.now());
        contentFilterMapper.updateById(filter);
    }

    @Override
    @Transactional
    public void deleteContentFilter(Long filterId) {
        contentFilterMapper.deleteById(filterId);
    }

    @Override
    public boolean isContentFiltered(String userId, String contentType, String contentValue) {
        QueryWrapper<ContentFilter> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId)
                .eq("filter_type", contentType)
                .eq("filter_value", contentValue)
                .eq("is_enabled", true);
        return contentFilterMapper.selectCount(wrapper) > 0;
    }

    @Override
    public List<String> getRecommendedContent(String userId, int page, int size) {
        // 获取用户偏好
        List<UserPreference> preferences = getUserPreferences(userId);
        
        // 获取用户浏览历史
        List<BrowseHistory> history = getBrowseHistory(userId, 1, 100);
        
        // 获取用户屏蔽规则
        List<ContentFilter> filters = getContentFilters(userId);
        
        // TODO: 实现基于用户偏好、浏览历史和屏蔽规则的推荐算法
        // 这里需要根据具体业务需求实现推荐逻辑
        return new ArrayList<>();
    }

    @Override
    @Transactional
    public void updateContentPreference(String userId, String contentType, String contentId, boolean isPositive) {
        UserPreference preference = new UserPreference();
        preference.setUserId(userId);
        preference.setPreferenceType(contentType);
        preference.setPreferenceValue(contentId);
        preference.setIsEnabled(true);
        preference.setDescription(isPositive ? "Positive feedback" : "Negative feedback");
        
        saveUserPreference(preference);
    }
} 